package com.tuanzi.loan.business.service.receipt.product.impl;

import com.tuanzi.loan.business.entity.receipt.product.ReceiptProductPrice;
import com.tuanzi.loan.business.repository.receipt.product.ReceiptProductPriceRespository;
import com.tuanzi.loan.business.service.receipt.product.ReceiptProductPriceService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.vo.request.receipt.product.ReceiptProductPriceSearchRequest;
import com.tuanzi.loan.vo.response.receipt.product.ReceiptProductPriceSearchResponse;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Debug
@Slf4j
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class ReceiptProductPriceServiceImpl extends BaseAuditServiceImpl<ReceiptProductPrice, Long> implements ReceiptProductPriceService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ReceiptProductPriceRespository receiptProductPriceRespository;


    @Override
    protected JPAAuditRepository<ReceiptProductPrice, Long> getRepository() {
        return this.receiptProductPriceRespository;
    }

    @Override
    public List<ReceiptProductPriceSearchResponse> findAllVoByCondition(ReceiptProductPriceSearchRequest searchCondition) {
        return receiptProductPriceRespository.findAllVoByCondition(searchCondition);
    }

    @Override
    public List<ReceiptProductPrice> findAllPoByCondition(ReceiptProductPriceSearchRequest searchCondition) {
        return receiptProductPriceRespository.findAllPoByCondition(searchCondition);
    }

    @Override
    public List<ReceiptProductPrice> findPriceByApplyRangeCoincide(ReceiptProductPriceSearchResponse productPrice) {
        return receiptProductPriceRespository.findPriceByApplyRangeCoincide(productPrice);
    }

    @Override
    public void saveOrUpdatePrice(ReceiptProductPriceSearchResponse editRowVo,ReceiptProductPriceSearchRequest searchCondition) {
        ReceiptProductPrice editRowPo = null;
        List<ReceiptProductPrice> updateList = new ArrayList<>();
        if (editRowVo.getId()==null) {
            editRowPo = new ReceiptProductPrice();
            editRowPo.setProductId(searchCondition.getProductId());
        }else{
            //如果是修改操作，则对两边的元素进行同步修改。
            List<ReceiptProductPrice> receiptProductPriceList = findAllPoByCondition(searchCondition);
            for (ReceiptProductPrice price : receiptProductPriceList) {
                if (price.getId().equals(editRowVo.getId())) {
                    editRowPo = price;
                    int editRowIndex = receiptProductPriceList.indexOf(editRowPo);
                    int maxIndex = receiptProductPriceList.size()-1;
                    if (editRowIndex+1<= maxIndex) {
                        ReceiptProductPrice nextRowPrice = receiptProductPriceList.get(editRowIndex + 1);
                        nextRowPrice.setMinApply(editRowVo.getMaxApply()+1);
                        updateList.add(nextRowPrice);
                    }
                    if (editRowIndex-1>=0) {
                        ReceiptProductPrice preRowPrice = receiptProductPriceList.get(editRowIndex - 1);
                        preRowPrice.setMaxApply(editRowVo.getMinApply()-1);
                        updateList.add(preRowPrice);
                    }
                }
            }
        }
        if (editRowPo!=null) {
            editRowPo.setMaxApply(editRowVo.getMaxApply());
            editRowPo.setMinApply(editRowVo.getMinApply());
            editRowPo.setUnitPrice(editRowVo.getUnitPrice());
            editRowPo.setRemark(editRowVo.getRemark());
            updateList.add(editRowPo);
            saveOrUpdate(updateList);
            String ladderPriceKey = LoanRedisKey.LOAN_RECEIPT_PRODUCT_LADDER_PRICE + editRowVo.getProductId();
            stringRedisTemplate.delete(ladderPriceKey);
        }
    }

    @Override
    public void deletePrice(Long deleteId,ReceiptProductPriceSearchRequest searchCondition) {
        List<ReceiptProductPrice> receiptProductPriceList = findAllPoByCondition(searchCondition);
        ReceiptProductPrice deleteRow = null;
        ReceiptProductPrice updateRow = null;
        for (ReceiptProductPrice productPrice : receiptProductPriceList) {
            if (deleteId.equals(productPrice.getId())) {
                deleteRow = productPrice;
                int deleteRowIndex = receiptProductPriceList.indexOf(deleteRow);
                int maxIndex = receiptProductPriceList.size() - 1;
                if (deleteRowIndex + 1 <= maxIndex) {
                    updateRow = receiptProductPriceList.get(deleteRowIndex + 1);
                    updateRow.setMinApply(deleteRow.getMinApply());
                }
            }
        }
        if (deleteRow != null) {
            delete(deleteRow);
            String ladderPriceKey = LoanRedisKey.LOAN_RECEIPT_PRODUCT_LADDER_PRICE + deleteRow.getProductId();
            stringRedisTemplate.delete(ladderPriceKey);
            if (updateRow != null) {
                saveOrUpdate(updateRow);
            }
        }
    }




}
